<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片区域-->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="问后台为什么搜索没啥卵用？"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--表格区域-->
      <el-table :data="orderlist" border style="width: 100%" stripe>
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="280px"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status==='0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-tooltip effect="dark" content="查看物流进度" placement="top" :enterable="false">
              <el-button
                type="success"
                icon="el-icon-location"
                size="mini"
                @click="showKuaidiDialog(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!--修改地址对话框-->
    <el-dialog title="修改地址" width="50%" :visible.sync="editDialogVisible" @close="editDialogClose">
      <!--修改地址表单-->
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressInfo">确 定</el-button>
      </span>
    </el-dialog>
    <!--查看物流对话框-->
    <el-dialog title="查看物流信息（模拟数据）" width="50%" :visible.sync="kuaidiDialogVisible">
      <!--时间线-->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in kuaidiData"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata.js'
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      orderlist: [],
      total: 0,
      editDialogVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      cityData: cityData,
      kuaidiDialogVisible: false,
      kuaidiData: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res && res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.orderlist = res.data.goods
      this.total = res.data.total
    },
    // 每页条数变化
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getOrderList()
    },
    // 页数变化
    handleCurrentChange(newCurrentPage) {
      this.queryInfo.pagenum = newCurrentPage
      this.getOrderList()
    },
    showEditDialog(orderId) {
      this.editDialogVisible = true
    },
    editDialogClose() {
      this.$refs.addressFormRef.resetFields()
    },
    editAddressInfo() {
      this.$refs.addressFormRef.validate(validate => {
        if (!validate) {
          return
        }
        this.editDialogVisible = false
        this.$alert('<strong>假装已经完成了修改，正在静静地坐着发呆</strong>', '好无聊啊', {
          dangerouslyUseHTMLString: true
        })
      })
    },
    async showKuaidiDialog(row) {
      this.kuaidiDialogVisible = true
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res && res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.kuaidiData = res.data
    }
  }
}
</script>
<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
.el-cascader {
  width: 100%;
}
</style>
